package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.ContainerForm;
import com.vcaresolution.logistic.pojo.User;
import com.vcaresolution.logistic.to.ContainerTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>ContainerController</code> is used to track all container related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class ContainerController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.CONTAINERFORM_MODELATTRIBUTE)
	public ContainerForm createForm(){
		return new ContainerForm();
	}
	
	private void loadRequiredDetailInModelObject(ModelAndView modelAndView){
		modelAndView.addObject(ApplicationConstants.VOYAGELIST_MODELATTRIBUTE, getServiceRegistry().getVoyageService().findVoyageWithVessel());
		modelAndView.addObject(ApplicationConstants.PORTLIST_MODELATTRIBUTE, getServiceRegistry().getPortService().findByAll());
		
		modelAndView.addObject(ApplicationConstants.CONTAINERSIZELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("CONTAINERTYPE"));
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDContainer Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_CONTAINER_URL,method = RequestMethod.GET)
	public ModelAndView loadAddContainer() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_CONTAINER_VIEW);
		modelAndView.addObject(ApplicationConstants.CONTAINERFORM_MODELATTRIBUTE, new ContainerForm());
		
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}

	/**
	 * This method is called when the POST request has been made to the ADDContainer Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_CONTAINER_URL,method = RequestMethod.POST)
	public ModelAndView processAddContainer(@Valid ContainerForm containerForm, 
			 final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		if( !result.hasErrors() ){
			User loggedInUser = getLoggedInUser(request);
			
			containerForm.setUser(loggedInUser);
			getServiceRegistry().getContainerService().add(containerForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_CONTAINER_URL, true, true, false));
		} else {
			loadRequiredDetailInModelObject(modelAndView);
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ListContainer Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_CONTAINER_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<ContainerTO> containerList = getServiceRegistry().getContainerService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.CONTAINERLIST_MODELATTRIBUTE, containerList);
		modelAndView.setViewName(ApplicationConstants.LIST_CONTAINER_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDContainer Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_CONTAINER_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditContainer(@RequestParam(value="containerId") String containerId) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object containerObject = getServiceRegistry().getContainerService().findById(Integer.parseInt(containerId));
		loadRequiredDetailInModelObject(modelAndView);
		
		modelAndView.addObject(ApplicationConstants.CONTAINERFORM_MODELATTRIBUTE, containerObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_CONTAINER_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDContainer Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_CONTAINER_URL,method = RequestMethod.POST)
	public ModelAndView processEditContainer(@Valid ContainerForm containerForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			
			containerForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getContainerService().update(containerForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_CONTAINER_URL, true, true, false));
		} else {
			
			loadRequiredDetailInModelObject(modelAndView);	
		}		
		return modelAndView;
	}
}